package com.hzbank.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import com.hzbank.dao.UserDao;
import com.hzbank.dto.User;

@Service
public class UserService{
    @Autowired
    private UserDao userDao;
    
    public boolean insert(User user) {
        boolean falg=false;
        try{
            userDao.save(user);
            falg=true;
        }catch(Exception e){
            e.printStackTrace();
        }
        return falg;
    }

    public List<User> search(String searchContent) {
          QueryStringQueryBuilder builder = new QueryStringQueryBuilder(searchContent);
          SearchQuery build = new NativeSearchQueryBuilder().withQuery(builder).build();
          TermQueryBuilder termQuery = QueryBuilders.termQuery("name", "吴奥杰");
          Iterable<User> search = userDao.search(termQuery);
          Iterable<User> searchResult = userDao.search(build);
          Iterator<User> iterator = searchResult.iterator();
          List<User> list=new ArrayList<User>();
          while (iterator.hasNext()) {
            list.add(iterator.next());
          }
       return list;
    }
    
    
    
    public List<User> searchUser(Integer pageNumber, Integer pageSize,String searchContent) {
//      Page<User> searchPageResults = userDao.search(builder, pageable);
//      System.out.println("查询的语句:" + searchPageResults.toString());
         // 分页参数
        Pageable pageable = new PageRequest(pageNumber, pageSize);
        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(searchContent);
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withPageable(pageable).withQuery(builder).build();
        System.out.println("查询的语句:" + searchQuery.toString());
        Page<User> searchPageResults = userDao.search(searchQuery);
        return searchPageResults.getContent();
    }
    

    public List<User> searchUserByWeight(String searchContent) {
     // 根据权重进行查询
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
                .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("name", searchContent)),
                    ScoreFunctionBuilders.weightFactorFunction(10))
                .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("description", searchContent)),
                        ScoreFunctionBuilders.weightFactorFunction(100)).setMinScore(2);
        System.out.println("查询的语句:" + functionScoreQueryBuilder.toString());
        Iterable<User> searchResult = userDao.search(functionScoreQueryBuilder);
        Iterator<User> iterator = searchResult.iterator();
        List<User> list=new ArrayList<User>();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }
    /**
     * 查询全部
     * @return
     */
    public List<User> matchfindall(){
    	/*MatchAllQueryBuilder allUser = new MatchAllQueryBuilder();
    	System.out.println("查询的语句:" + allUser.toString());
    	Iterable<User> all = userDao.search(allUser);
    	List<User> list=new ArrayList<User>();
    	for (User user : all) {
    		list.add(user);
		}*/
    	List<User> list=new ArrayList<User>();
    	Iterable<User> findAll = userDao.findAll();
    	for (User user : findAll) {
    		list.add(user);
		}
    	
    	return list;
    }

	public List<User> compoundquery(String name,String createtm) {
//		BoolQueryBuilder must = QueryBuilders.boolQuery().must(QueryBuilders.queryStringQuery("吴奥杰"));
		BoolQueryBuilder must = QueryBuilders.boolQuery().must(QueryBuilders.fuzzyQuery("name", name)).must(QueryBuilders.queryStringQuery(createtm).field("createtm"));
		Iterable<User> search = userDao.search(must);
		List<User> list=new ArrayList<User>();
		for (User user : search) {
			list.add(user);
		}
//		List<User> findByNameLike = userDao.findByNameLike(name);
		return list;
	}
    
	public List<User> compoundquery2(String name,String description) {
		return userDao.findByNameAndDescriptionLikeOrderByIdDesc(name,description);
	}
	
	
	
	
	
	
	
}